import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBConnector {
	public static Connection connect() {
		Connection conn = null;

		try {
			String userName = "sa";
			String password = "Password101#";
			String url = "jdbc:sqlserver://EN30109\\SQLEXPRESS";
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
			conn = DriverManager.getConnection(url, userName, password);
			System.out.println("Database connection established");
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
			System.err.println(e.getMessage());
			System.err.println(e.getStackTrace());
		}
		return conn;
	}

	public static void insertPlayer(Player player, Connection conn) {
		PreparedStatement  s = null;
		try {
//			s = conn.prepareStatement("SET NAMES 'cp1251';");
//			s.execute();
			insertPlayer(conn, player);
			if (s!=null){
				s.close();
			}
			System.out.println("Rows were inserted");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void insertPlayer(Connection conn,Player player){
		try {
			PreparedStatement s = conn.prepareStatement("INSERT INTO  pefl.dbo.player ("+
					"name ,"+
					"age ,"+
					"country ,"+
					"nominal ,"+
					"zarplata ,"+
					"ani_zp ,"+
					"position ,"+
					"liderstvo ,"+
					"udary ,"+
					"videnie ,"+
					"navesy ,"+
					"reactia ,"+
					"strafnye ,"+
					"igra_rukami ,"+
					"moshi ,"+
					"rabota ,"+
					"dribling ,"+
					"pas ,"+
					"vyhody ,"+
					"dalinie ,"+
					"skorosti ,"+
					"vybor ,"+
					"tehnica ,"+
					"otbor ,"+
					"vynos ,"+
					"personalka ,"+
					"uglovye ,"+
					"golova ,"+
					"champ_games ,"+
					"champ_goals ,"+
					"champ_passes ,"+
					"champ_im ,"+
					"champ_mark"+
					")"+
					"VALUES ("+
					"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
			s.setString(1, player.getName());
			s.setInt(2, player.getAge());
			s.setString(3, player.getNationality());	//			"country ,"+
			s.setInt(4,player.getNominal());	//			"nominal ,"+
			s.setInt(5, 0);	//			"zarplata ,"+
			s.setInt(6, 0);	//			"ani_zp ,"+
			s.setString(7, player.getPosition());	//			"position ,"+
			s.setInt(8,player.getLiderstvo());	//			"liderstvo ,"+
			s.setInt(9,player.getUdary());	//			"udary ,"+
			s.setInt(10,player.getVidenie());	//			"videnie ,"+
			s.setInt(11,player.getNavesy());	//			"navesy ,"+
			s.setInt(12,player.getReactia());	//			"reactia ,"+
			s.setInt(13,player.getShtrafnye());	//			"strafnye ,"+
			s.setInt(14,player.getRuki());	//			"igra_rukami ,"+
			s.setInt(15,player.getMoshi());	//			"moshi ,"+
			s.setInt(16,player.getRabota());	//			"rabota ,"+
			s.setInt(17,player.getDribling());	//			"dribling ,"+
			s.setInt(18,player.getPas());	//			"pas ,"+
			s.setInt(19,player.getVyhody());	//			"vyhody ,"+
			s.setInt(20,player.getDalinie());	//			"dalinie ,"+
			s.setInt(21,player.getSkorosti());	//			"skorosti ,"+
			s.setInt(22,player.getVybor());	//			"vybor ,"+
			s.setInt(23,player.getTehnica());	//			"tehnica ,"+
			s.setInt(24,player.getOtbor());	//			"otbor ,"+
			s.setInt(25,player.getVynos());	//			"vynos ,"+
			s.setInt(26,player.getPersonalka());	//			"personalka ,"+
			s.setInt(27,player.getUglovye());	//			"uglovye ,"+
			s.setInt(28,player.getGolova());	//			"golova ,"+
			s.setInt(29,0);	//			"champ_games ,"+
			s.setInt(30,0);	//			"champ_goals ,"+
			s.setInt(31,0);	//			"champ_passes ,"+
			s.setInt(32,0);	//			"champ_im ,"+
			s.setInt(33,0);	//			"champ_mark"+
			s.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void disconnect(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
				System.out.println("Database connection terminated");
			} catch (Exception e) { /* ignore close errors */
			}
		}
	}
}
